package com.aaa.service.impl;

import com.aaa.entity.Oder;
import com.aaa.entity.Order;
import com.aaa.entity.Orderdetails;
import com.aaa.mapper.*;
import com.aaa.service.OrderService;
import com.aaa.utils.GainDate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class OrderServiceImpl implements OrderService {
    @Resource
    private OrderMapper orderMapper;
    @Resource
    private RoomMapper roomMapper;
    @Resource
    private OrderdetailsMapper orderdetailsMapper;
    @Resource
    private CheckMapper checkMapper;
    @Resource
    private BillMapper billMapper;
    @Override
    public List<Map<String, Object>> loadfind(String state, String ordernum, String rtid) throws ParseException {
        List<Map<String,Object>> list=orderMapper.loadfind(state,ordernum,rtid);
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date now=new Date();
        if(list.size()>0&&list!=null){
            System.out.println(list.size());
            for (int i = 0; i < list.size(); i++) {
                System.out.println(list.get(i));
                Date  gracepays=null;
                Date enddate=null;
                if(list.get(i).get("state").equals("待入住")){
                    enddate=  sdf.parse(list.get(i).get("enddate").toString());
                    System.out.println(now.getTime()>=enddate.getTime());
                    if(now.getTime()>=enddate.getTime()){
                        Integer id= (Integer) list.get(i).get("id");
                        System.out.println("超时订单："+id);
                        //取消超时未入住订单
                        orderMapper.OverTimeState(id,"已取消");
                        list=orderMapper.loadfind(state,ordernum,rtid);
                    }
                }
                if(list.get(i).get("state").equals("待付款")){
                    gracepays=  sdf.parse(list.get(i).get("gracepay").toString());
                    if(now.getTime()>=gracepays.getTime()){
                        Integer id= (Integer) list.get(i).get("odid");
                        //取消超时未支付订单
                        orderMapper.OverTimeState(id,"已取消");
                        list=orderMapper.loadfind(state,ordernum,rtid);
                    }
                }
            }
            return list;
        }
        return list;
    }

    @Override
    public Integer changemoney(double money, Integer oid, Integer rid, Integer staff) {
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date now=new Date();
        List<Map<String ,Object>> list1=orderdetailsMapper.findodid(rid);
        //修改子订单状态
        orderdetailsMapper.checkoutstate((Integer) list1.get(0).get("id"));
        //添加账单操作
        billMapper.addbill("退房",money,(Integer) list1.get(0).get("id"),"", staff);
        //修改实际退房时间
        checkMapper.checkoutstate((Integer) list1.get(0).get("id"),sdf.format(now));
        List<Map<String ,Object>> list2=orderdetailsMapper.findStateByOid((Integer) list1.get(0).get("orderid"));
        //子订单是否全部完成
        boolean tag=false;
        if(list2.size()>0&&list2!=null){
            for (int i = 0; i < list2.size(); i++) {
                if(list2.get(i).get("state").equals("已完成")){
                    //子订单完成
                    tag=true;
                }else {
                    //子订单未完成
                    tag=false;
                }
            }
        }
        //若子订单全部完成改变订单状态
        if(tag){
            orderMapper.checkoutState((Integer) list1.get(0).get("orderid"));
        }
        //修改房间状态
        roomMapper.checkout(rid);
        //改变订单金额
        return orderMapper.changemoney(money,oid);
    }

    /**
     * 查询当前一周营业额
     * @return
     */
    @Override
    public List<Map> week() {
        List<Map> list=new ArrayList<>();
        List<Oder> listto= orderMapper.week();
        for (Oder oder : listto) {
            Map map=new HashMap();
            map.put("date", GainDate.getWeekOfDate(oder.getPaydate()));
            map.put("money",oder.getMoney());
            list.add(map);
        }
        return list;
    }

    /**
     * 返回上周营业额
     * @return
     */
    @Override
    public List<Map> lastweek() {
        List<Map> list=new ArrayList<>();
        List<Oder> listto= orderMapper.lastweek();
        for (Oder oder : listto) {
            Map map=new HashMap();
            map.put("paydate", GainDate.getWeekOfDate(oder.getPaydate()));
            map.put("money",oder.getMoney());
            list.add(map);
        }
        return list;
    }

    /**
     * 查询本月所有收入
     * @return
     */
    @Override
    public List<Map> month() {
        List<Map> list=new ArrayList<>();
        List<Oder> listto= orderMapper.month();
        for (Oder oder : listto) {
            Map map=new HashMap();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss",Locale.getDefault());
            Date date = oder.getPaydate();
            String py=sdf.format(date);
            String pydate = py.substring(5, 10);
            //System.out.println(pydate);
            map.put("paydate", pydate);
            map.put("money",oder.getMoney());
            list.add(map);
        }
        return list;
    }

    @Override
    public List<Map> some(String begin, String finish) {
        List<Map> mapList=new ArrayList<>();
        List<Oder> some = orderMapper.some(begin, finish);
        for (Oder oder : some) {
            Map map=new HashMap();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss",Locale.getDefault());
            Date date = oder.getPaydate();
            String py=sdf.format(date);
            String pydate = py.substring(0, 10);
            System.out.println(pydate);
            map.put("paydate", pydate);
            map.put("money",oder.getMoney());
            mapList.add(map);
        }
        return mapList;
    }

    @Override
    public List<Map<String, Object>> finddate(Integer tid) {
        return orderMapper.finddate(tid);
    }



    @Override
    public Object checkmoney(String enddate, Integer rid) throws ParseException {
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        enddate=enddate.replace("T"," ").substring(0,19);
        //开始时间
        Date now = new Date();
        //结束时间
        Date endDate = sdf.parse(enddate);
        long num=( endDate.getTime()-now.getTime())/(1000L*3600L*24L);
        System.out.println(num);
//
        System.out.println(String.valueOf(( endDate.getTime()-now.getTime())/(1000L*3600L*24L)));
        System.out.println(Double.parseDouble(String.valueOf(( endDate.getTime()-now.getTime())/(1000L*3600L*24L))));
        double betweenDate=Double.parseDouble(String.valueOf(( endDate.getTime()-now.getTime())/(1000L*3600L*24L)));
        betweenDate=betweenDate+1;
        System.out.println("betweenDate"+betweenDate);

        Map<String,Object> map=roomMapper.findnewmoney(rid);
        double money= (Integer.parseInt(map.get("money").toString())) * betweenDate;
        return money;
    }

    @Override
    public Object checkinOrder(Integer staff, String ordernum, String name, String phone, String idcard, Integer rid, String money, String startdate, String enddate, String state) {
        Map<String,Object> map=roomMapper.findrt(rid);
        Integer rtid= (Integer) map.get("tid");
        Order order=new Order(ordernum,name,phone,startdate,enddate,rtid,money,state);
        orderMapper.checkinOrder(order);
        Integer orderid=order.getId();
        System.out.println(orderid);
        Integer roomid=rid;
        Orderdetails orderdetails=new Orderdetails(orderid,roomid,rtid,state);
        orderdetailsMapper.checkinOrderdetails(orderdetails);
        Integer odid=orderdetails.getId();
        System.out.println(odid);
        checkMapper.checkin(name,phone,idcard,startdate,enddate,odid,staff);
        roomMapper.checkin(rid,state);
        return roomMapper.checkin(rid,state);
    }

}
